package com.jred.jforum.actions.message;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.jred.jforum.IConst;
import com.jred.jforum.actions.common.TDDispatchAction;
import com.jred.jforum.beans.forum.Forum;
import com.jred.jforum.beans.forum.Message;
import com.jred.jforum.beans.forum.Topic;
import com.jred.jforum.beans.user.User;
import com.jred.jforum.exception.SystemException;
import com.jred.jforum.forms.ProcessMessageForm;
import com.jred.jforum.service.ForumService;

public class MessageManagerAction extends TDDispatchAction {

	/**
	 * 简单回复
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws SystemException
	 */
	public ActionForward addThread(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws SystemException {
		HttpSession session = request.getSession();
		ForumService forumService = (ForumService) getServiceFacade("forumService");
		ProcessMessageForm pmForm = (ProcessMessageForm) form;
		String fid = pmForm.getFid();

		User user = (User) session.getAttribute(IConst.SESSION.USER_KEY);
		if (user == null) {
			return (mapping.findForward("jforum-denied"));
		}
		Message msg = new Message();
		msg.setSender(user.getName());
		msg.setContents((String) request.getParameter("contents"));
		msg.setHeading((String) request.getParameter("heading"));
		msg.setIntime(new Date());
		msg.setIp("127.0.0.1");

		forumService.addMessage(msg);

		StringBuffer sb = new StringBuffer();
		sb.append("/ShowForum.do?fid=");
		sb.append(fid);
		return (new ActionForward(sb.toString(), true));
	}

	public ActionForward addInit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws SystemException {
		HttpSession session = request.getSession();
		ForumService forumService = (ForumService) getServiceFacade("forumService");
		ProcessMessageForm pmForm = (ProcessMessageForm) form;

		Forum currForum = forumService.getForumInfo(Integer.parseInt(pmForm
				.getFid()));

		User user = (User) session.getAttribute(IConst.SESSION.USER_KEY);
		if (user == null) {
			return (mapping.findForward("jforum-denied"));
		}
		
		Topic currThread = forumService.getThreadInfo(Integer.parseInt(pmForm
				.getTid()));

		session.setAttribute(IConst.REQUEST.CURR_THREAD, currThread);
		Message mess = forumService.getMessageInfo(Integer.parseInt(pmForm
				.getMid()));
		if (mess == null) {
			return (new ActionForward("/ShowThread.do?fid=" + pmForm.getFid()
					+ "&tid=" + pmForm.getTid()));
		} else {
			request.setAttribute("message", mess);
		}
		request.setAttribute("JFORUM_CURR_FORUM", currForum);
		return (mapping.findForward("addThread"));
	}

}
